﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magols72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using betfairfind.Betfair.Global;
using betfairfind.Betfair.UK;

namespace betfairfind
{
    public static class BF
    {

        static public betfairfind.Betfair.Global.APIRequestHeader HEADER_GLOBAL = new betfairfind.Betfair.Global.APIRequestHeader();
        static public betfairfind.Betfair.UK.APIRequestHeader HEADER_UK = new betfairfind.Betfair.UK.APIRequestHeader();
        static public BFExchangeService client = new BFExchangeService();
        static public betfairfind.Betfair.Global.BFGlobalService client_global = new betfairfind.Betfair.Global.BFGlobalService();


        static public ViewProfileResp GetProfile()
        {
            ViewProfileReq req = new ViewProfileReq();
            req.header = HEADER_GLOBAL;
            try
            {
                ViewProfileResp resp = client_global.viewProfile(req);
                return resp;
            }
            catch (Exception ex)
            {

                Logger.Log(ex.ToString(), true, true);
            }
            return null;
        }

        static public MarketPrices getMarketPrices(int marketId)
        {

            GetMarketPricesReq priceReq = new GetMarketPricesReq();
            GetMarketPricesResp priceResp = new GetMarketPricesResp();
            priceReq.header = HEADER_UK;
            priceReq.marketId = marketId;
            try {
                priceResp =  client.getMarketPrices(priceReq);
                return priceResp.marketPrices;          
            }
            catch (Exception ex)
            {

                Logger.Log(marketId + "--" + ex.ToString(), true, true);
                return null;
            }

        }

        static public Market getMarket(int marketId)
        {

            GetMarketReq req = new GetMarketReq();
            GetMarketResp res = new GetMarketResp();
            req.header = HEADER_UK;
            req.marketId = marketId;
            try
            {
                res = client.getMarket(req);
                return res.market;
            }
            catch (Exception ex)
            {

                Logger.Log(marketId + "--" + ex.ToString(), true, true);
                return null;
            }

        }


        // daystofetch can be null = no  limit
        static public MyMarket[] RequestAllMarkets(Nullable<int>[] sportids, double hourstofetch)
        {

            GetAllMarketsReq marketsReq = new GetAllMarketsReq();
            marketsReq.header = HEADER_UK;
            marketsReq.eventTypeIds = sportids; // only get soccer
            marketsReq.fromDate = DateTime.Now; //  new DateTime(2007, 12, 21);
            marketsReq.toDate = DateTime.Now.AddHours(hourstofetch);
            MyMarket[] markets = null; 
            try
            {
                GetAllMarketsResp marketsResp = client.getAllMarkets(marketsReq);
                if (marketsResp.marketData != "")
                {
                    markets = parseMarkets(marketsResp.marketData);
                }
            }
            catch (Exception ex)
            {

                Logger.Log(ex.ToString(), true, true);
            }

            return markets;
        }


        static public MarketPricesCompressed GetMarketPricesCompressed(int marketId) 
        {  
            GetMarketPricesCompressedReq req = new GetMarketPricesCompressedReq();
            req.header = BF.HEADER_UK;
            req.marketId = marketId;
            MarketPricesCompressed mpc = null;

            try 
	            {	        
		                GetMarketPricesCompressedResp resp = BF.client.getMarketPricesCompressed(req);
                        mpc = new MarketPricesCompressed(resp);
	            }
            catch (Exception ex)
            {

                Logger.Log(ex.ToString(), true, true);
	        }
            return mpc;
        }
    



        // parsing the string data result from a "Get All Markets" request
        static public MyMarket[] parseMarkets(string marketData)
        {
            
            string[] marketPcs;
            MyMarket parsing = new MyMarket();
            // remove first :
            marketData = marketData.Substring(1, marketData.Length - 1);
            string[] strMarkets = marketData.Split(':');
            MyMarket[] allMarkets = new MyMarket[strMarkets.Length];

                for (int i = 0; i < strMarkets.Length; i++)
                {   
                   try
                    {


                    marketPcs = strMarkets[i].Split('~');
                    parsing = new MyMarket();
                    parsing.marketId = int.Parse(marketPcs[0]);
                    parsing.name = marketPcs[1];
                    //         parsing.marketType =  marketPcs[2];           krångel
                    //          parsing.marketStatus = marketPcs[3];k         rångek

                 
                        DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
                        dt = dt.AddMilliseconds(long.Parse(marketPcs[4]));
                 //       dt = dt.AddHours(2);                                                         // skillnad mot sverige
                        parsing.marketTime = dt;



                    parsing.menuPath = marketPcs[5];
                    parsing.menuPath.Replace("\\", "\\\\");
                    //           parsing.eventHierarchy = marketPcs[6];        krångel
                    parsing.marketDelay = int.Parse(marketPcs[7]);
                    parsing.licenceId = int.Parse(marketPcs[8]);
                    parsing.countryISO3 = marketPcs[9];
                    parsing.lastRefresh = long.Parse(marketPcs[10]);
                    //            parsing.runners = marketPcs[11];                krångel         
                    parsing.numberOfWinners = int.Parse(marketPcs[12]);

                    parsing.marketTotalMatched = double.Parse(marketPcs[13].Replace('.', ','));
                    if (marketPcs[14].Equals("Y"))
                    {
                        parsing.bspMarket = true;
                    }
                    else
                    {
                        parsing.bspMarket = false;
                    }

                    if (marketPcs[15].Equals("Y"))
                    {
                        parsing.marketGoingInPlay = true;
                    }
                    else
                    {
                        parsing.marketGoingInPlay = false;
                    }

                    allMarkets[i] = parsing;
                    }
                   catch (Exception)
                   {

               //        Logger.Log(ex.ToString());
                   }
                }

            return allMarkets;
        }
        
        /*

        static public BFUK.Market getMarket(int marketId)
        {
            BFSoccer.betfair.UK.GetMarketReq marketReq = new BFSoccer.betfair.UK.GetMarketReq();
            BFSoccer.betfair.UK.GetMarketResp marketResp = new BFSoccer.betfair.UK.GetMarketResp();
            marketReq.header = HEADER_UK;
            marketReq.marketId = marketId;
            marketReq.includeCouponLinks = true;
            marketResp = bfUK.getMarket(marketReq);
            return marketResp.market;
        }
          * */

        static public string convertPathToGame(string menuPath)
        {
            string[] parts = menuPath.Split('\\');
            return parts[parts.Length - 1];
        }

    }
}
